package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.desensitization.annotation.SensitiveEncode;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppUserZhifubao;
import org.jeecg.modules.demo.hd.mapper.AppUserZhifubaoMapper;
import org.jeecg.modules.demo.hd.service.IAppUserZhifubaoService;
import org.jeecg.modules.demo.hd.vo.zhifubao.addZhiFuBaoVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 支付宝账户管理
 * @Author: jeecg-boot
 * @Date:   2025-02-09
 * @Version: V1.0
 */
@Service
public class AppUserZhifubaoServiceImpl extends ServiceImpl<AppUserZhifubaoMapper, AppUserZhifubao> implements IAppUserZhifubaoService {

    @Override
    public boolean addZhifubao(addZhiFuBaoVo vo) {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        AppUserZhifubao model=new AppUserZhifubao();
        model.setAccount(vo.getAccount());
        model.setAccountStatus("1");
        model.setUserId(currentUser.getId());
        if(!this.checkAccount(vo.getAccount(),currentUser.getId()))
        {
            this.baseMapper.insert(model);
        }
        return true;
    }

    @Override
    @SensitiveEncode
    public String queryCurrentZhifubao() {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<AppUserZhifubao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", currentUser.getId());
        queryWrapper.eq("account_status", "1");
        List<AppUserZhifubao> appUserZhifubaos = this.baseMapper.selectList(queryWrapper);
        if(ObjectUtils.isNotNull(appUserZhifubaos)&& appUserZhifubaos.size()>0)
        {
            return appUserZhifubaos.get(0).getAccount();
        }
        return  "";
    }

    private boolean checkAccount(String account,String userId)
    {
        QueryWrapper<AppUserZhifubao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("account", account);
        int count = this.baseMapper.selectCount(queryWrapper).intValue();
        return count>0;
    }
}
